package com.jackykeke.kdatabasedemo.db.helper

import android.content.Context
import android.database.sqlite.SQLiteDatabase

class GameDBHelper : BaseDatabaseHelper {

    private var mContext: Context? = null

    private val TAG = "GameDBHelper"

    companion object {
        /**
         * 数据库当前版本号(数据库每次升级该字段加1)<br></br>
         * start at 25<br></br>
         */
        val DB_VERSION = 26

        /**
         * 数据库名
         */
        val DATABASE_NAME = "gamelib.db"
    }


    constructor(context: Context?) : super(
        context,
        DATABASE_NAME,
        DB_VERSION
    ) {
        mContext = context


//        // XXX 官方建议在异步线程调用getWritableDatabase()或者getReadableDatabase()，因为可能长期执行
//        // NOTE:https://developer.android.com/training/basics/data-storage/databases.html
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
        var db: SQLiteDatabase? = null
        try {
            db = writableDatabase
            if (!mIsUpgrade) { // 更新失败，则删除数据库，再行创建。
                db?.close()
                mContext!!.deleteDatabase(DATABASE_NAME)
                writableDatabase
            }
        } catch (e: Exception) {
            mContext!!.deleteDatabase(DATABASE_NAME)
        }

    }


    override fun onCreate(db: SQLiteDatabase?) {

        db?.beginTransaction()

        db?.endTransaction()

    }

    override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {

    }


}